package com.example.a2207_zh.register

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.a2207_zh.entity.LoginEntityResult
import com.example.a2207_zh.mvvm.BaseViewModel
import com.google.gson.Gson
import kotlinx.coroutines.launch
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody

class RegisterViewModel:BaseViewModel() {
    val success = MutableLiveData<LoginEntityResult>()
    val fail = MutableLiveData<String>()
    fun register(username:String,password:String,repassword:String){
        viewModelScope.launch {
            try {
                val map = mutableMapOf<String,String>()
                map["username"] = username
                map["password"] = password
                map["repassword"] = repassword
                val json = Gson().toJson(map)
                val body = RequestBody.create("application/json;charset=utf-8".toMediaType(),json)
                success.value = apiServer.register(body)
            }catch (e:Exception){
                fail.value = e.message
            }
        }
    }


}